//
//  UserModel.m
//  RainbowFM
//
//  Created by chenli on 14/12/17.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "UserModel.h"
#import "LoginModel.h"
#import "UserAPI.h"
#import "FileUpLoadAPI.h"

@implementation UserModel

+(instancetype)sharedModel
{
    static UserModel *userModel = nil;
    static dispatch_once_t once;
    
    dispatch_once(&once, ^{
        userModel = [[UserModel alloc] init];
    });
    
    return userModel;
    
}


-(void)fetchUserProfile:(void(^)(BOOL))result{
    __block UserAPI *api = [[UserAPI alloc] init];
    [api fetchUserProfileWithUserID:[LoginModel sharedModel].userID result:^(NSDictionary *dict, NSString *msg) {
        if (dict != nil) {
            self.userInfo = [[UserItem alloc] initWithAttributes:dict];
            if (result != nil) {
                result(YES);
            }
            
        }
        
        api = nil;
    }];
}


-(void)editUserProfileNickName:(NSString *)nickName reuslt:(void (^)(BOOL))res{
    __block UserAPI *api = [[UserAPI alloc] init];
    [api editUserProfileWithUserID:[LoginModel sharedModel].userID mgrField:@"nickName" userTel:nil
                        userOldPwd:nil
                  smsValidateToken:nil
                           userPwd:nil
                          nickName:nickName
                               sex:nil
                          birthDay:nil
                           address:nil
                              sign:nil
                           headPic:nil
                            result:^(BOOL success) {
                                res(success);
                            }];
}

-(void)editUserProfileSex:(NSString *)sex result:(void (^)(BOOL))res{
    __block UserAPI *api = [[UserAPI alloc] init];
    [api editUserProfileWithUserID:[LoginModel sharedModel].userID mgrField:@"sex" userTel:nil
                        userOldPwd:nil
                  smsValidateToken:nil
                           userPwd:nil
                          nickName:nil
                               sex:sex
                          birthDay:nil
                           address:nil
                              sign:nil
                           headPic:nil
                            result:^(BOOL success) {
                                res(success);
                            }];
}

-(void)editUserProfileBirthday:(NSString *)birthday reuslt:(void (^)(BOOL))res{
    __block UserAPI *api = [[UserAPI alloc] init];
    [api editUserProfileWithUserID:[LoginModel sharedModel].userID mgrField:@"birthday" userTel:nil
                        userOldPwd:nil
                  smsValidateToken:nil
                           userPwd:nil
                          nickName:nil
                               sex:nil
                          birthDay:birthday
                           address:nil
                              sign:nil
                           headPic:nil
                            result:^(BOOL success) {
                                res(success);
                            }];
}


-(void)editUserProfileArea:(NSString *)area result:(void (^)(BOOL))res{
    __block UserAPI *api = [[UserAPI alloc] init];
    [api editUserProfileWithUserID:[LoginModel sharedModel].userID mgrField:@"address" userTel:nil
                        userOldPwd:nil
                  smsValidateToken:nil
                           userPwd:nil
                          nickName:nil
                               sex:nil
                          birthDay:nil
                           address:area
                              sign:nil
                           headPic:nil
                            result:^(BOOL success) {
                                res(success);
                            }];

}

-(void)editUserProfileSign:(NSString *)sign result:(void (^)(BOOL))res{
    __block UserAPI *api = [[UserAPI alloc] init];
    [api editUserProfileWithUserID:[LoginModel sharedModel].userID mgrField:@"sign" userTel:nil
                        userOldPwd:nil
                  smsValidateToken:nil
                           userPwd:nil
                          nickName:nil
                               sex:nil
                          birthDay:nil
                           address:nil
                              sign:sign
                           headPic:nil
                            result:^(BOOL success) {
                                res(success);
                            }];
}


-(void)editUserProfileHeadImage:(NSString *)imagePath result:(void (^)(BOOL))res{
    __block UserAPI *api = [[UserAPI alloc] init];
    [api editUserProfileWithUserID:[LoginModel sharedModel].userID mgrField:@"headPic" userTel:nil
                        userOldPwd:nil
                  smsValidateToken:nil
                           userPwd:nil
                          nickName:nil
                               sex:nil
                          birthDay:nil
                           address:nil
                              sign:nil
                           headPic:imagePath
                            result:^(BOOL success) {
                                res(success);
                            }];
}

-(void)upLoadUserHeadImage:(NSData *)imageData result:(void (^)(BOOL,NSString *))res{
    FileUpLoadAPI *api = [[FileUpLoadAPI alloc] init];
    [api upLoadFileWithServerName:@"" URL:HEAD_UPLOAD_URL fileData:imageData fileType:@"jpg" result:^(BOOL success,NSString *path,NSString *msg) {
        if (success) {
            [self editUserProfileHeadImage:path result:^(BOOL success) {
               [self fetchUserProfile:^(BOOL success) {
                   if (success) {
                       res(YES,path);
                   }
               }];
                
            }];
        }
    }];
}


-(void)fetchUserAddressListWithPage:(int)page result:(void (^)(BOOL, NSArray *))res{
    __block UserAPI *api = [[UserAPI alloc] init];
    [api fetchUserAddressWithUserID:[LoginModel sharedModel].userID currentPage:[NSString stringWithFormat:@"%d",page] pageSize:@"100" result:^(NSArray *array, NSString *msg) {
        NSMutableArray *listArray = [NSMutableArray array];
        for (NSDictionary *dict in array) {
            [listArray addObject:[[AddressListItem alloc] initWithAttributes:dict]];
        }
        
        res(YES,listArray);
    }];
}


-(void)addAddressWithDeliName:(NSString *)name deliTel:(NSString *)tel deliAreaAddr:(NSString *)area deliDtlAddr:(NSString *)addr result:(void (^)(BOOL))res{
    __block UserAPI *api = [[UserAPI alloc] init];
    [api userManageAddressWithOper:@"1"
                            deliID:@""
                            userID:[LoginModel sharedModel].userID
                          deliName:name
                           deliTel:tel
                      deliAreaAddr:area
                       deliDtlAddr:addr
                            result:^(BOOL success) {
                                res(success);
                            }];
}


-(void)editAddressWithDeliName:(NSString *)name deliID:(NSString *)deliId deliTel:(NSString *)tel deliAreaAddr:(NSString *)area deliDtlAddr:(NSString *)addr result:(void (^)(BOOL))res{
    __block UserAPI *api = [[UserAPI alloc] init];
    [api userManageAddressWithOper:@"2"
                            deliID:deliId
                            userID:[LoginModel sharedModel].userID
                          deliName:name
                           deliTel:tel
                      deliAreaAddr:area
                       deliDtlAddr:addr
                            result:^(BOOL success) {
                                res(success);
                            }];
}


-(void)removeAddressWithDeliID:(NSString *)Id result:(void (^)(BOOL))res{
    __block UserAPI *api = [[UserAPI alloc] init];
    [api userManageAddressWithOper:@"3"
                            deliID:Id
                            userID:[LoginModel sharedModel].userID
                          deliName:@""
                           deliTel:@""
                      deliAreaAddr:@""
                       deliDtlAddr:@""
                            result:^(BOOL success) {
                                res(success);
                            }];
}


-(void)fetchUserAddressDetailWithDeliID:(NSString *)Id result:(void (^)(BOOL, NSDictionary *))res{
    __block UserAPI *api = [[UserAPI alloc] init];
    [api fetchUserAddressDetailWithUserID:[LoginModel sharedModel].userID deliID:Id result:^(NSDictionary *dict, NSString *msg) {
        if (dict != nil) {
            res(YES,dict);
        }
    }];
}


@end




@implementation UserItem

-(instancetype)initWithAttributes:(NSDictionary *)attributes{
    self = [super init];
    if (!self) {
        return nil;
    }
    
    _address  = attributes[@"address"];
    _birthday = attributes[@"birthday"];
    _headPic  = attributes[@"headPic"];
    _nickName = attributes[@"nickName"];
    _sex      = attributes[@"sex"];
    _sign     = attributes[@"sign"];
    _userId   = attributes[@"userId"];
    _userTel  = attributes[@"userTel"];
    
    
    return self;
}
@end


@implementation AddressListItem

-(instancetype)initWithAttributes:(NSDictionary *)attributes{
    self = [super init];
    if (!self) {
        return nil;
    }
    
    _deliAddr = attributes[@"deliAddr"];
    _deliId   = attributes[@"deliId"];
    _deliName = attributes[@"deliName"];
    _deliTel  = attributes[@"deliTel"];
    
    
    return self;
}

@end